package com.travel.server.manager.impl.command;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.server.manager.dto.request.PageRequest;
import com.travel.server.manager.dto.request.project.TimeRequest;
import com.travel.server.manager.impl.base.BaseCommand;
import com.travel.server.manager.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/18 11:03
 * @Desc:
 */
public abstract class AbstractCommand<T1, T2> extends BaseCommand {

    /**
     * 检查入参
     * @param request
     */
    protected abstract void checkInput(T1 request);

    /**
     * 检查分页入参
     * @param request
     */
    protected void checkPageInput(PageRequest request){
        if(request.getLimit() == null || request.getPage() == null){
            throwInvalidParam();
        }
    }

    protected void checkTimeInput(TimeRequest request){
        if(StringUtils.isBlank(request.getStartTime()) || StringUtils.isBlank(request.getEndTime())){
            throwInvalidParam();
        }
    }

    /**
     * 执行方法
     * @param request
     * @return
     */
    public abstract T2 handle(T1 request);

    protected void setAttribute(String key, Object value){
        HttpServletRequest request = getHttpServletRequest();
        request.getSession().setAttribute(key, value);
    }

    protected void throwInvalidParam(){
        throw new WorkException(WorkCode.INVALID_PARAM);
    }

}
